﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace WebAPI.Filters
{
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
    public class KeywordFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // 获取用户请求的参数
            var parameters = filterContext.ActionArguments;//filterContext.ActionParameters;
            // 过滤参数中的关键字
            foreach (var parameter in parameters)
            {
                if (parameter.Value is not string) continue;
                var value = parameter.Value.ToString();
                if (value == null || !value.Contains("1")) continue;
                filterContext.Result = new ContentResult { Content = "请求参数中包含非法字符！" };
                break;
            }
            base.OnActionExecuting(filterContext);
        }
    }
}